﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonTools;
using shop.Model;


namespace shop.Bll.BasicInfo
{

	/// <summary> 部门 基础资料 </summary>
	public class JDepartmentBll : BasicBllParent, IFastQueryBll<JDepartment>
	{

		#region 实现IFastQueryBll 接口, 在FastBox中使用

		/// <summary> 本地存储的明细 </summary>
		/// <remarks>如果基础资每次都从数据库查询, 这个没用, 保持null</remarks>
		public List<JDepartment> Items => null;


		/// <summary> 是否每次从数据库查询 </summary>
		public bool QueryFromDataBase => true;

		
		/// <summary> 存储在快速查询窗体中,显示的属性,及属性的显示, 这个暂时不用,以后删除 </summary>
		public Dictionary<string, string> DisplayColumns => null;


		/// <summary> 显示的列, 静态 </summary>
		private static readonly string[] _columns = new string[] { nameof(JDepartment.Code), nameof(JDepartment.Name) };


		/// <summary> 存储在快速查询窗体中,显示的属性 </summary>
		public string[] Columns => _columns;


		/// <summary> 实体模型的代码属性名称 </summary>
		public string CodeField => nameof(JDepartment.Code);



		/// <summary> 代码中级别的分隔符 </summary>
		public override string Separator => ".";



		/// <summary> 查询树形图的所有节点 </summary>
		public override IEnumerable<IFastTree> QueryTreeList()
		{
			var rst = DB.FSql.Select<JDepartment>().Where(a => a.IsDetails == false && a.Isdel == false);

			return rst.ToList().ToList<IFastTree>();
		}


		/// <summary> 查询方法 </summary>
		public List<JDepartment> Query(string findText)
		{
			return DB.FSql.Select<JDepartment>().Where(a => a.Isdel == false && a.IsDetails == true
											&& (a.Name.Contains(findText) || a.Code.Contains(findText)))
				.ToList().OrderBy(a => a.Code).ToList();
		}


		/// <summary> 查询树形图的一个节点下的所有内容 </summary>
		public List<JDepartment> QueryByGroup(string group)
		{
			var list = DB.FSql.Select<JDepartment>()
					.Where(w => w.IsDetails == true && w.Isdel == false
					&& w.Code.StartsWith(group));

			return list.ToList().OrderBy(a => a.Code).ToList();        //排序
		}

		public List<JDepartment> Query(string findText, string prefixCode = null)
		{
			return this.Query(findText);      //已排序
		}


		#endregion

		#region 权限

		/// <summary> 查询导出权限 </summary>
		public override string LookRight => "01.04.1";

		/// <summary> 保存权限 </summary>
		public override string SaveRight => "01.04.2";

		/// <summary> 修改别人单据权限 </summary>
		public string ModifyRight => "01.04.3";

		/// <summary> 删除别人单据权限 </summary>
		public string DeleteRight => "01.04.4";

		/// <summary> 审核,反审核权限 </summary>
		public override string CheckRight => "01.04.5";

		public string Title => "部门信息";

		#endregion

		#region 其他
		/// <summary> 使用代码查询一个 </summary>
		/// 
		public JDepartment GetByCode(string code)
		{
			return DB.FSql.Select<JDepartment>()
					.Where(w => w.IsDetails == true && w.Isdel == false && w.Code == code).ToOne();
		}


		/// <summary>
		/// 部门信息查询
		/// </summary>
		/// <param name="code"></param>
		/// <param name="isDetails"></param>
		/// <param name="Msg"></param>
		/// <returns></returns>
		public override IEnumerable<object> Query(string code, bool isDetails, out string Msg)
		{
			Msg = null;
			var Departments = DB.FSql.Select<JDepartment>().Where(s => s.IsDetails == isDetails && s.Isdel == false);

			if (string.IsNullOrEmpty(code) == false)
			{
				Departments = Departments.Where(a => a.Code.StartsWith(code));
			}

			return Departments.ToList();

		}
		/// <summary>
		/// 部门分级增加
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="user"></param>
		/// <param name="Msg"></param>
		/// <returns></returns>
		public override bool AddGroup(object obj, IUser user, out string Msg)
		{
			Msg = null;
			JDepartment model = obj as JDepartment;
			if (obj == null)
			{
				Msg = "b:转型失败";
				return false;
			}

			var id = DB.FSql.Insert(model).ExecuteIdentity();
			model.ID = (int)id;

			return true;
			 
		}
		/// <summary>
		/// 部门分级删除
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="user"></param>
		/// <param name="Msg"></param>
		/// <returns></returns>
		public override bool DeleteGroup(object obj, IUser user, out string Msg)
		{
			Msg = null;
			JDepartment department = obj as JDepartment;
			if (department == null)
			{
				Msg = "b:转型失败";
				return false;
			}
			if (department.Code.Length <= 0)
			{
				Msg = "代码不能为空";
			}

			department.Isdel = true;
			DB.FSql.Update<JDepartment>()
				.Where(department)
				.Set(a => a.Isdel, true)
				.ExecuteAffrows();

			return true;
			 
		}

		/// <summary> 修改分组 </summary>
		public override bool UpdateGroup(object obj, IUser user, out string err)
		{
			err = "";
			if (!(obj is JDepartment model))
			{
				err = Model.CN.Msg_InformationCannotBeBlank; // "辅助资料为空";
				return false;
			}

			throw new NotImplementedException();
			//return dal.ClassModify(model, user, out err);
		}


		/// <summary>
		/// 部门新增明细
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="user"></param>
		/// <param name="Msg"></param>
		/// <returns></returns>
		public override bool Insert(object obj, IUser user, out string Msg)
		{
			Msg = null;
			JDepartment Department = obj as JDepartment;
			Department.IsDetails = true;
			Department.Isdel = false;
			Department.IsDisabled = false;

			if (obj == null)
			{
				Msg = "b:转型失败";
				return false;
			}
			if (Department == null)
			{
				Msg = "b:对象为空";
				return false;
			}
			if (Department.Code == null || Department.Code.Length <= 0)
			{
				Msg = "b:代码不能为空";
				return false;
			}
			if (Department.Name == null || Department.Name.Length <= 0)
			{
				Msg = "b:名称不能为空";
				return false;
			}
			if (this.GetByCode (Department.Code) != null )
			{
				Msg = "b:代码不能重复";
				return false;
			}

			var id = DB.FSql.Insert(Department).ExecuteIdentity();
			Department.ID = (int)id;

			return true;
			 
		}
		/// <summary>
		/// 部门明细修改
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="user"></param>
		/// <param name="Msg"></param>
		/// <returns></returns>
		public override bool Update(object obj, IUser user, out string Msg)
		{
			Msg = null;
			JDepartment Department = obj as JDepartment;
			if (obj == null)
			{
				Msg = "b:转型失败";
				return false;
			}
			if (Department.ID <= 0)
			{
				Msg = "b:仓库ID为空";
				return false;
			}
			if (Department == null)
			{
				Msg = "b:仓库对象为空";
				return false;
			}
			if (Department.Code == null || Department.Code.Length <= 0)
			{
				Msg = "b:仓库代码不能为空";
				return false;
			}
			if (Department.Name == null || Department.Name.Length <= 0)
			{
				Msg = "b:仓库名称不能为空";
				return false;
			}

			DB.FSql.Update<JDepartment>()
				.SetSource(Department)
				.ExecuteAffrows();

			return true;
			 
		}
		/// <summary>
		/// 部门明细删除
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="user"></param>
		/// <param name="Msg"></param>
		/// <returns></returns>
		public override bool Delete(object obj, IUser user, out string Msg)
		{
			Msg = null;
			JDepartment Department = obj as JDepartment;
			if (obj == null)
			{
				Msg = "b:转型失败";
				return false;
			}
			//if (Department.ID <= 0)
			//{
			//    Msg = "b:仓库ID为空";
			//    return false;
			//}
			//if (Department == null)
			//{
			//    Msg = "b:仓库对象为空";
			//    return false;
			//}
			//if (Department.Code == null || Department.Code.Length <= 0)
			//{
			//    Msg = "b:仓库代码不能为空";
			//    return false;
			//}
			//if (Department.Name == null || Department.Name.Length <= 0)
			//{
			//    Msg = "b:仓库名称不能为空";
			//    return false;
			//}

			Department.Isdel = true;
			DB.FSql.Update<JDepartment>()
			   .SetSource(Department)
			   .Set(a => a.Isdel, true)
			   .ExecuteAffrows();

			return true;

		}

		public override bool Check(object obj, IUser user, out string err)
		{
			throw new NotImplementedException();
		}

		public override bool UnCheck(object obj, IUser user, out string err)
		{
			throw new NotImplementedException();
		}




		#endregion


	}
}



 
